import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:search_people/utils/component/selection/model.dart';

import 'index.dart';

class SelectionController extends GetxController {
  SelectionController();

  final state = SelectionState();

  void firstColumnChooseAction({
    required int idx,
    required SelectionModel md,
  }) {
    for (var i = 0; i < state.list.length; i++) {
      state.list[i].isSelected = i == idx;
    }
    state.firstIndex = idx;
    update();
    if (md.children.isEmpty) {
      if (md.requestData != null) {
        md.requestData!();
      }
    }
  }

  void secondColumnChooseAction({
    required int idx,
    required SelectionModel md,
  }) {
    state.secondIndex = idx;
    update();
  }

  void secondColumnItemChooseAction({
    required int idx,
    required SelectionModel md,
  }) {
    if (state.secondIndex == idx) {
      md.isSelected = !md.isSelected;
    } else {
      md.isSelected = true;
    }

    if (md.children.isNotEmpty) {
      for (var element in md.children) {
        element.isSelected = md.isSelected;
      }
    }
    chooseEvent();
    state.secondIndex = idx;
    update();
  }

  void thirdColumnChooseAction({
    required int idx,
    required SelectionModel md,
  }) {
    state.thirdIndex = idx;
    update();
  }

  void thirdColumnItemChooseAction({
    required int idx,
    required SelectionModel md,
  }) {
    md.isSelected = !md.isSelected;

    if (md.isSelected) {
      state.list[state.firstIndex].children[state.secondIndex!].isSelected =
          true;
    } else {
      state.list[state.firstIndex].children[state.secondIndex!].children
          .firstWhere(
        (element) => element.isSelected,
        orElse: () {
          state.list[state.firstIndex].children[state.secondIndex!].isSelected =
              false;
          return md;
        },
      );
    }
    chooseEvent();
    state.thirdIndex = idx;
    update();
  }

  void chooseEvent() {
    var e1 = state.list[state.firstIndex];
    var str = '';
    for (var i = 0; i < e1.children.length; i++) {
      var m1 = e1.children[i];
      if (!m1.isSelected) {
        continue;
      }
      str += '${m1.title}(';
      var smd = SectionIndex()
        ..index = i
        ..name = m1.title
        ..val = m1.value ?? '';
      for (var j = 0; j < m1.children.length; j++) {
        var m2 = m1.children[j];
        if (m2.isSelected) {
          var smd2 = SectionIndex()
            ..index = j
            ..name = m2.title
            ..val = m2.value ?? '';
          smd.idxs.add(smd2);
          str += '${m2.title}/';
        }
      }
      str = str.substring(0, str.length - 1);
      str += ')/';
    }
    if (str.isNotEmpty) {
      str = '${e1.title}：$str';
      str = str.substring(0, str.length - 1);
      e1.result = str;
    } else {
      e1.result = null;
    }

    e1.searchText = null;
  }

  void tagItemChooseAction({
    required List<int> idxs,
    required List<SelectionModel> mds,
  }) {
    var e1 = state.list[state.firstIndex];
    if (mds.isEmpty) {
      e1.result = null;
    } else {
      var str = '${e1.title}：';

      str += mds.map((e) => e.title).join('/');
      e1.result = str;
    }

    update();
  }

  void actionReset() {
    state.secondIndex = null;
    state.thirdIndex = null;
    state.selectedIndexs = {};
    for (var e1 in state.list) {
      e1.result = null;
      for (var e2 in e1.children) {
        e2.isSelected = false;
        for (var e3 in e2.children) {
          e3.isSelected = false;
        }
      }
    }
    update();
  }

  void actionSure() {
    state.selectedIndexs = {};
    bool hasConditions = false;
    for (var k = 0; k < state.list.length; k++) {
      var e1 = state.list[k];
      state.selectedIndexs[k] = [];
      for (var i = 0; i < e1.children.length; i++) {
        var m1 = e1.children[i];
        if (!m1.isSelected) {
          continue;
        }
        hasConditions = true;
        var smd = SectionIndex()
          ..index = i
          ..name = m1.title
          ..val = m1.value ?? '';
        for (var j = 0; j < m1.children.length; j++) {
          var m2 = m1.children[j];
          if (m2.isSelected) {
            var smd2 = SectionIndex()
              ..index = j
              ..name = m2.title
              ..val = m2.value ?? '';
            smd.idxs.add(smd2);
          }
        }
        state.selectedIndexs[k]!.add(smd);
      }
    }
    if (!hasConditions) {
      EasyLoading.showToast('请选择筛选条件');
      return;
    }

    if (state.onSelected != null && state.selectedIndexs.isNotEmpty) {
      state.onSelected!(state.selectedIndexs);
    }
  }

  void actionSearch({
    required String text,
    required List<SelectionModel> list,
  }) {
    var str = text.removeAllWhitespace;
    if (str.isEmpty) {
      state.list[state.firstIndex].searchResult = null;
    } else {
      state.list[state.firstIndex].searchResult = list
          .where((element) =>
              element.title.contains(str) ||
              element.value?.contains(str) == true)
          .toList();
    }

    update();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.rc = RefreshController();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.rc?.dispose();
    state.rc = null;
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
